﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Licensed MIT: http://www.opensource.org/licenses/mit-license.php

using System;

namespace DNA.Mvc
{
    /// <summary>
    /// Describe the web resource info.
    /// </summary>
    public class WebResourceInfo:INavigable
    {
        /// <summary>
        /// Gets/Sets the resource uri.
        /// </summary>
        public Uri Url { get; set; }

        public WebResourceInfo(string url)
        {
            Url = new Uri(url);
        }

        public WebResourceInfo(Uri url) { Url = url; }

        /// <summary>
        /// Gets the resource owner
        /// </summary>
        public string Owner
        {
            get
            {
                if (Url.IsAbsoluteUri)
                {
                    if (Url.Host.ToLower() == "home")
                        return "";
                    else
                        return Url.Host;
                }
                return "";
            }
        }

        private string contentType = "";

        /// <summary>
        /// Gets the resource content type.
        /// </summary>
        public string ContentType 
        {
            get 
            {
                if (IsFile)
                {
                    string mime = "application/octetstream";
                    Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(Extension);
                    if (rk != null && rk.GetValue("Content Type") != null)
                        mime = rk.GetValue("Content Type").ToString();
                    contentType= mime;
                }
                return contentType;
            }
        }
       
        /// <summary>
        /// Gets/Sets the resource size.
        /// </summary>
        public long FileSize 
        {
            get 
            {
                return filesize;
            }
            set 
            {
                filesize = value;
            }
        }
        private long filesize = 0;

        
        

        /// <summary>
        /// Gets wheather this resource is point to a file.
        /// </summary>
        public bool IsFile
        { 
            get{ return !string.IsNullOrEmpty(Extension);}
        }

        /// <summary>
        /// Gets the resource file name or path name.
        /// </summary>
        public string Name { get { return System.IO.Path.GetFileName(Url.ToString()); } }

        /// <summary>
        /// Gets the resource file extension.
        /// </summary>
        public string Extension 
        {
            get 
            {
                return System.IO.Path.GetExtension(Url.LocalPath.ToString()).ToLower(); 
            } 
        }

        string INavigable.Title
        {
            get { return this.Name;}
        }

        string INavigable.Description
        {
            get { return this.ContentType; }
        }

        string INavigable.NavigateUrl
        {
            get { return this.Url.ToString(); }
        }

        string INavigable.ImageUrl
        {
            get { return ""; }
        }

        string INavigable.Target
        {
            get { return "_self"; }
        }

        object INavigable.Value
        {
            get { return this.Url; }
        }
    }
}
